#include Lib\MemoryFunctions.ahk

#NoEnv  ; Recommended for performance and compatibility with future AutoHotkey releases.
SendMode Input  ; Recommended for new scripts due to its superior speed and reliability.

	rezz() {

	GlobalVars()
	Global mpthreshold
	
	mpdeficittotal:=getMPDeficit()
	dead:=getDeadMercs()
	tooltip, dead %dead% mpdeficit %mpdeficittotal% mpthreshold %mpthreshold% ,450,0,5
	
	
	
	while (getDeadMercs()>0 && !inBattle()) {
		WinGetActiveStats, Title, ScreenWidth, ScreenHeight, posix, posiy
		send {Space}
		sleep, 200
		lifepotionx=0
		revivex=0
		ImageSearch, lifepotionx, lifepotiony, 218, 120, 1057, 498, *30 Images\Items\lifepotion.png 
		if (lifepotionx>0) {
			MouseClick, right, lifepotionx, lifepotiony 
		}
		else {
			ImageSearch, lifepotionx, lifepotiony, 218, 120, 1057, 498, *30 Images\Items\lifepotion2.png 
			if (lifepotionx>0) {
				MouseClick, right, lifepotionx, lifepotiony  
			}
			else {
				ImageSearch, lifepotionx, lifepotiony, 218, 120, 1057, 498, *75 Images\Items\lifepotion3.png 
				if (lifepotionx>0) {
					MouseClick, right, lifepotionx, lifepotiony
				}
			}
		}
		send {Space}
		now:=A_now
		elapsed:=0
		while (elapsed<2){
			closeShit()
			sleep, 100
			ImageSearch, revivex, revivey, 250, 250, ScreenWidth-300, ScreenHeight-200, *30 Images\Misc\reviver.png
			ImageSearch, revivetodosx, revivetodosy, 250, 250, ScreenWidth-300, ScreenHeight-200, *30 Images\Misc\revivertodos.png
			EnvSub, elapsed, now, seconds
			if (revivex>0) { 
				MouseClick, left, revivex, revivey
				now:=A_now
				sleep, 1000
			}
			if (revivetodosx>0){ 
				MouseClick, left, (revivetodosx+139), (revivetodosy-154)
				now:=A_now
				sleep, 1000
			}
		}
	}
	
	now:=A_now
	elapsed:=0
	
	send {Space}
	
	while (getMPDeficit()>mpthreshold && elapsed<4) {
		EnvSub, elapsed, now, seconds
		ImageSearch, mpfoodx, mpfoody,218, 120, 829, 550, *70 Images\Items\carpa.png
		if (ErrorLevel=0) {
			MouseClick, right, mpfoodx, mpfoody, 2
		}
		else {
			ImageSearch, mpfoodx, mpfoody, 218, 120, 829, 550, *70 Images\Items\carneiro.png
			if (ErrorLevel=0) {
				MouseClick, right, mpfoodx, mpfoody, 2
				
			}
			else {
				ImageSearch, mpfoodx, mpfoody, 218, 120, 829, 550, *70 Images\Items\porcogrelhado.png
				if (ErrorLevel=0) {
					MouseClick, right, mpfoodx, mpfoody, 2
					
				}
				else {
					ImageSearch, mpfoodx, mpfoody, 218, 120, 829, 550, *70 Images\Items\seafood.png
					if (ErrorLevel=0) {
						MouseClick, right, mpfoodx, mpfoody, 2
					}
				}
			}
		}
		sleep, 500
	}
	
send {Space}
}

	

	rezzMin() {

	GlobalVars()
	Global mpthreshold
	global active_id
	ControlSend, , {f1}, ahk_id %active_id%
	
	mpdeficittotal:=getMPDeficit()
	dead:=getDeadMercs()
	tooltip, dead %dead% mpdeficit %mpdeficittotal% mpthreshold %mpthreshold% ,450,0,5
	
	
	
	while (getDeadMercs()>0) {
		WinGetActiveStats, Title, ScreenWidth, ScreenHeight, posix, posiy
		sleep, 200
		lifepotionx=0
		revivex=0
		ImageSearch, lifepotionx, lifepotiony, 0, 0, ScreenWidth, ScreenHeight, *30 Images\Items\lifepotion.png 
		ControlSend, , 1, ahk_id %active_id%
		sleep, 10
		ControlSend, , {f1}, ahk_id %active_id%
		now:=A_now
		elapsed:=0
		while (elapsed<2 && getDeadMercs!=0){
			ControlClick, x593 y295,ahk_id %active_id%,,left,1,na
			EnvSub, elapsed, now, seconds
			sleep, 300
		}
	}
	
		
		
	
	now:=A_now
	elapsed:=0
	
	
	
	while (getMPDeficit()>mpthreshold && elapsed<2) {
		EnvSub, elapsed, now, seconds
		
		ControlSend, , {f2}, ahk_id %active_id%
		sleep, 500
		}
	}

	getMercName(){
		
		if (isMainActive()) {
			return "main"
		}
		else if(isMonkActive()){
			return "monk"
		}
		else if(isArtActive()){

			return "art"
		}
		else if(isElementalistaActive()){
			return "elementalista"
			}
		else if(isProphetActive()){
			return "prophet"
		}
		else if(isOracleActive()){
			return "oracle"
		}
		else if(isJaniActive()){
			return "jani"
		}
		else if(isInventorActive()){
			return "inventor"
		}
		else if (isSwordActive()){
			return "sword"
		}
		else if (isCanhoneiroActive()){
			return "canhoneiro"
		}
		else if (isBTActive()){
			return "bt"
		}
		else if(isPrincessActive()){
			return "princess"
		}
		else if(isHwarangActive()){
			return "hwarang"
		}
		else if(isVikingActive()){
			return "viking"
		}
		else if(isSpartanActive()){
			return "spartan"
		}
		else if(isLKActive())	{
			return "lk"
		}
		else if(isExoActive())	{
			return "exo"
		}
		else if(isTarkanActive())	{
			return "tarkan"
		}
		else if(isSheriffActive())	{
			return "sheriff"
		}
		else if (isChampionActive()) {
			return "champion"
		}
		else if (isWitchActive()) {
			return "witch"
		}
		else if (isDruidActive()) {
			return "druid"
		}
		else if (isVampireActive()) {
			return "vampire"
		}
		else {
			return "unknown"
		}
	
	}
	

	
	isSpellAvail(merc){
		if (merc="main") {
			Return isMainSpellAvail()
		}
		else if (merc="elementalista") {
			return isElementalistaSpellAvail()
		}
		else if (merc="canhoneiro") {
			return isCanhoneiroSpellAvail()
		}
		else if (merc="art") {
			return isArtSpellAvail()
		}
		else if (merc="monk") {
			return isMonkSpellAvail()
		}
		else if (merc="viking") {
			return isVikingSpellAvail()
		}
		else if (merc="bt") {
			return isBTSpellAvail()
		}
		else if (merc="prophet") {
			return isProphetSpellAvail()
		}
		else if (merc="oracle") {
			return isOracleSpellAvail()
		}
		else if (merc="spartan") {
			return isSpartanSpellAvail()
		}
		else if (merc="witch") {
			return isWitchSpellAvail()
		}
		else {
			return 0
		}
	}
	
	
	isMainSpellAvail(){
		ImageSearch, px, py, 932, 798, 1151, 839, *30 Images\Spells\RM.png
		if (px>0) {
			return 1
		}
		else {
			ImageSearch, px, py, 932, 798, 1151, 839, *50 Images\Spells\TD.png
			if (px>0) {
				return 1
			}
		}
	return 0
	}
	
	isWitchSpellAvail(){
		ImageSearch, px, py, 932, 798, 1151, 839, *30 Images\Spells\meteor.png
		if (px>0) {
			return 1
		}
	return 0
	}
	
	isVikingSpellAvail(){
		ImageSearch, px, py, 932, 798, 1151, 839, *30 Images\Spells\IR.png
		if (px>0) {
			return 1
		}
		else {
			ImageSearch, px, py, 932, 798, 1151, 839, *30 Images\Spells\FA.png
			if (px>0) {
				return 1
			}
		}
	return 0
	}
	
	
	isElementalistaSpellAvail() {
		ImageSearch, px, py, 932, 798, 1151, 839, *30 Images\Spells\TE.png
		if (px>0) {
			return 1
		}
	return 0
	}
	
	isCanhoneiroSpellAvail() {
		ImageSearch, px, py, 932, 798, 1151, 839, *30 Images\Spells\SBomb.png
		if (px>0) {
			return 1
		}
	return 0
	}
	
	isProphetSpellAvail() {
		ImageSearch, px, py, 932, 798, 1151, 839, *30 Images\Spells\SS.png
		if (px>0) {
			return 1
		}
	return 0
	}
	
	isArtSpellAvail() {
		ImageSearch, px, py, 932, 798, 1151, 839, *80 Images\Spells\DI.png
		if (px>0) {
			return 1
		}
	return 0
	}
	
	isBTSpellAvail() {
		ImageSearch, px, py, 932, 798, 1151, 839, *30 Images\Spells\SB.png
		if (px>0) {
			return 1
		}
	return 0
	}

	isOracleSpellAvail() {
		ImageSearch, px, py, 932, 798, 1151, 839, *30 Images\Spells\heal.png
		if (px>0) {
			return 1
		}
	return 0
	}
	
	isSpartanSpellAvail() {
		ImageSearch, px, py, 932, 798, 1151, 839, *30 Images\Spells\WC.png
		if (px>0) {
			return 1
		}
	return 0
	}
	
	isMonkSpellAvail() {
		ImageSearch, px, py, 932, 798, 1151, 839, *30 Images\Spells\awaken.png
		if (px>0) {
			return 1
		}
	return 0
	}
	
	isCanhoneiroActive(){
	ImageSearch, px, py, 461, 762, 930, 883, *50 Images\Mercenary\canhoneiro.png
	if (ErrorLevel=0) {
		return, 1 
	} 
	return, 0
}	
	isBTActive(){
	ImageSearch, px, py, 461, 762, 930, 883, *50 Images\Mercenary\BT.png
	if (ErrorLevel=0) {
		return, 1 
	} 
	return, 0
}	
	
	isSwordActive(){
	ImageSearch, px, py, 461, 762, 930, 883, *50 Images\Mercenary\sword.png
	if (ErrorLevel=0) {
		return, 1 
	} 
	return, 0
}	

	isExoActive(){
	ImageSearch, px, py, 461, 762, 930, 883, *50 Images\Mercenary\exo.png
		if (ErrorLevel=0) {
			return, 1 
		} 
	return, 0
	}	
	
isSpartanActive(){
	ImageSearch, px, py, 461, 762, 930, 883, *50 Images\Mercenary\spartan.png
	if (ErrorLevel=0) {
		return, 1 
	} 
	return, 0
}
isChampionActive(){
	ImageSearch, px, py, 461, 762, 930, 883, *50 Images\Mercenary\champion.png
	if (ErrorLevel=0) {
		return, 1 
	} 
	return, 0
}
isDruidActive(){
	ImageSearch, px, py, 461, 762, 930, 883, *50 Images\Mercenary\druid.png
	if (ErrorLevel=0) {
		return, 1 
	} 
	return, 0
}
isWitchActive(){
	ImageSearch, px, py, 461, 762, 930, 883, *50 Images\Mercenary\witch.png
	if (ErrorLevel=0) {
		return, 1 
	} 
	return, 0
}
isVampireActive(){
	ImageSearch, px, py, 461, 762, 930, 883, *50 Images\Mercenary\vampire.png
	if (ErrorLevel=0) {
		return, 1 
	} 
	return, 0
}
	
	isHwarangActive(){
	ImageSearch, px, py, 461, 762, 930, 883, *50 Images\Mercenary\hwarang.png
	if (ErrorLevel=0) {
		return, 1 
	} 
	return, 0
}
	
	isPrincessActive(){
	ImageSearch, px, py, 461, 762, 930, 883, *50 Images\Mercenary\princess.png
	if (ErrorLevel=0) {
		return, 1 
	} 
	return, 0
}
	
	isInventorActive(){
	ImageSearch, px, py, 461, 762, 930, 883, *50 Images\Mercenary\inventor.png
	if (ErrorLevel=0) {
		return, 1 
	} 
	return, 0
}
	
	isJaniActive(){
	ImageSearch, px, py, 461, 762, 930, 883, *50 Images\Mercenary\jani.png
	if (ErrorLevel=0) {
		return, 1 
	} 
	return, 0
}
		
isProphetActive(){
	ImageSearch, px, py, 461, 762, 930, 883, *50 Images\Mercenary\prophet.png
	if (ErrorLevel=0) {
		return, 1 
	} 
	return, 0
}

isTarkanActive(){
	ImageSearch, px, py, 461, 762, 930, 883, *50 Images\Mercenary\tarkan.png
	if (ErrorLevel=0) {
		return, 1 
	} 
	return, 0
}

isSheriffActive(){
	ImageSearch, px, py, 461, 762, 930, 883, *50 Images\Mercenary\sheriff.png
	if (ErrorLevel=0) {
		return, 1 
	} 
	return, 0
}

	nextTarget(){
		static currentTarget=0
		Global target
		currentTarget++
		target1=%A_WorkingDir%\Images\Misc\target1.png
		target2=%A_WorkingDir%\Images\Misc\target2.png
		target3=%A_WorkingDir%\Images\Misc\target3.png
		target4=%A_WorkingDir%\Images\Misc\target4.png
		target5=%A_WorkingDir%\Images\Misc\target5.png
		target6=%A_WorkingDir%\Images\Misc\target6.png
		target7=%A_WorkingDir%\Images\Misc\target7.png
		target8=%A_WorkingDir%\Images\Misc\target8.png
		target9=%A_WorkingDir%\Images\Misc\target9.png
		target10=%A_WorkingDir%\Images\Misc\target10.png

		
		if (currentTarget=1) {
			target:=target1
			
		}
		Else if (currentTarget=2){
			target:=target2
			
		}
		Else if (currentTarget=3){
			target:=target3
		}
		else if (currentTarget=4){
			target:=target4
			
		}
		else if (currentTarget=5){
			target:=target5
			
		}
		else if (currentTarget=6){
			target:=target6
			
		}
		else if (currentTarget=7){
			target:=target7
			
		}
		else if (currentTarget=8){
			target:=target8
			
		}
		else if (currentTarget=9){
			target:=target9
			
		}
		else if (currentTarget=10){
			target:=target10
			
		}
		
		ifNotExist, %target%
				{
				target:=target1
				currentTarget=1
				}
		tooltip target: %target% , 775,0,7 
		
		}
		
	setTarget(x){
		Global target
		target1=%A_WorkingDir%\Images\Misc\target1.png
		target2=%A_WorkingDir%\Images\Misc\target2.png
		target3=%A_WorkingDir%\Images\Misc\target3.png
		target4=%A_WorkingDir%\Images\Misc\target4.png
		target5=%A_WorkingDir%\Images\Misc\target5.png
		target6=%A_WorkingDir%\Images\Misc\target6.png
		target7=%A_WorkingDir%\Images\Misc\target7.png
		target8=%A_WorkingDir%\Images\Misc\target8.png
		target9=%A_WorkingDir%\Images\Misc\target9.png
		target10=%A_WorkingDir%\Images\Misc\target10.png
		
		if (x=1) {
			target:=target1
			
		}
		Else if (x=2){
			target:=target2
			
		}
		Else if (x=3){
			target:=target3
		}
		else if (x=4){
			target:=target4
			
		}
		else if (x=5){
			target:=target5
			
		}
		else if (x=6){
			target:=target6
			
		}
		else if (x=7){
			target:=target7
			
		}
		else if (x=8){
			target:=target8
			
		}
		else if (x=9){
			target:=target9
			
		}
		else if (x=10){
			target:=target10
			
		}
		ifNotExist, %target%
				{
				target:=target1
				currentTarget=1
				}
		tooltip target: %target% , 775,0,7 
		
	}
		
		

	waitForTurn(){
	while (inBattle()){
		PixelSearch, corx, cory, 455, 90, 459, 95, 0xABE147, 20 
		PixelGetColor, cor, 459, 90
		CloseInventory()
		sleep, 5
		if (corx>0) {
			break
		}
	}
}

	
	closeMSG(){
		ImageSearch, px, py, 680, 401, 728, 452, *10 Images\Misc\closeMSG.png
		if (px>0) {
			MouseClick, left, (px+2), (py+2)
		}
	}
	
	
	minimizeExpGanha(){
		ImageSearch, px, py, 0, 0, 1000, 1000, *10 Images\Misc\expganha.png
		if (px>0) {
			MouseClick, left, (px+305), (py-18)
		}
	}
	
	fecharGD(){
		ImageSearch, px, py, 0, 0, 1200, 1200, *10 Images\Misc\fecharGD.png
		if (px>0) {
			MouseClick, left, px, py
		}
		ImageSearch, tx, ty, 0, 0, 1200, 1200, *10 Images\Misc\lvlup.png
		if (tx>0) {
			MouseClick, left, tx, ty
		}
	}
	
	fecharReward(){
		ImageSearch, px, py, 500, 500, 900, 900, *30 Images\Misc\reward.png
		if (px>0) {
			MouseClick, left, px, py
		}
	}
	


acceptInfo(){
	ImageSearch , px, py, 310, 310 , 450, 450, *10 Images\Misc\accept.png
	if (ErrorLevel=0) {
		MouseClick, left, px, py
		sleep, 1500
	}
	ImageSearch , px, py, 200, 200 , 600, 1000, *10 Images\Misc\confirmar.png
		if (ErrorLevel=0) {
			MouseClick, left, px, py
		}
 	}
	
closeShit(){
	ImageSearch , px, py, 310, 310 , 500, 500, *50 Images\Misc\dead.png
	if (ErrorLevel=0) {
		MouseClick, left, 623, 447
	}
	if (isMapOpen()){
		send, m
	}
	
	ImageSearch , px, py, 580, 324 , 620, 366, *50 Images\Misc\sim.png
	if (ErrorLevel=0) {
		MouseClick, left, 623, 447
	}
	
	acceptInfo()
	fecharGD()
	closeMSG()
	fecharReward()
	closeBook()
}


	amIDead(map, autox, autoy ) {
	static elapsed:=A_now
	static ldeath :=A_now 
	static deathc:=0
	
	if (map>-1) {
		if (map!=getMapId() && getMapId()!=0){
			rezz()
			if (getMapId()=1) {
				deathc--
			}
			EnvSub, elapsed, now, minutes  
		if (elapsed>20) {
			deathc:=1
			}
		else {
			deathc++       
		}
		ldeath:=A_now
		if (deathc=4) {
			Pause
		}
			sleep, 200
			if (map=232) {
				While (getMapId()!=232&&!inBattle()) {
					portToBlackKeep()
					sleep, 3000
				}
			}
			else if (map=344) {
				While (getMapId()!=344&&getMapId()!=344&&!inBattle()) {  ; TBS asia central
					tpToZone(25, 3)
					sleep, 3000
				}
			}
			else if (map=260||map=261) {
				While (getMapId()!=260&&getMapId()!=261&&!inBattle()) {
					tpToZone(21, 3)                      ; MUTANTES
					sleep, 3000
				}
			}
			else if (map=354) {
				While (getMapId()!=354&&!inBattle()) {
					tpToZone(20, 5)                     ; TBS SHOGUN 
					sleep, 3000
				}
			}
			else if (map=358) {
				While (getMapId()!=358&&!inBattle()) {
					tpToZone(24, 5)                    ; TBS FROZEN
					sleep, 3000
				}
			}
			else if (map=342) {
				While (getMapId()!=342&&!inBattle()) {
					tpToZone(23, 8)                    ; TBS DETROIT
					sleep, 3000
				}
			}
			else if (map=271 ) {
				while (getMapID()!=271&&!inBattle()) {
					tpToZone(24,6)                      ; ARVORES AZTEC
					sleep, 3000
				}
			}
			else if (map=264||map=265) {
				while (getMapID()!=265&&!inBattle()) {
					tpToZone(22,3)                      ; DALLAS MINERADORES
					sleep, 3000
				}
			}
			else if (map=346) {
				while (getMapID()!=346&&!inBattle()) {
					tpToZone(27,4)                      ; TBS 122
					sleep, 3000
				}
			}
			else if (map>0) {
				While (getMapId()!=map&&!inBattle()){
					AutoMove(autox,autoy,map)
					sleep, 500
				}
			}
			if (isMapOpen()){
				send, m
			}
			
			
			AutoMove(autox,autoy,map)
			sleep, 10000
			closeBook()
		
		sleep, 200
		
		}
	}
	
}

OpenBook() {
ImageSearch, px, py, 0, 0, 1000, 1000, *10 Images\Misc\fecharlivro.png
			if (ErrorLevel=1){
				send !j				
				
			}
}

closeBook() {
	ImageSearch, px, py, 0, 0, 1000, 1000, *10 Images\Misc\fecharlivro.png
			if (px>0){
				mouseclick, left, 222, 154
				sleep, 300
				send !j				
				
			}
	
	
}

	tpToHanyang() {
		while (getMapID()!=1 && !inBattle()) {
			OpenBook()
			sleep, 300
			MouseClick, left, 435, 153
			sleep, 200
			MouseClick, left, 547, 215
			sleep, 200
			MouseClick, left, 747, 632
			sleep, 500
			MouseClick, left, 604, 337 
		}
		automove(164,191,1)
		sleep, 5000
		CloseBook()
		
	}
	
	
	tpToZone(pri, sub) {
		
		OpenBook()
		sleep, 300
		;ImageSearch, px, py, 0, 0, 1000, 1000, *70 Images\Misc\campo.png
		;sleep, 100
		MouseClick, left, 361, 148
		sleep, 50
		MouseClick, left, 446, 246  ; clica no campo princ
		sleep, 50
		MouseClick, left, 436, 263, 60 ; poe no comeco da lista
		sleep, 200
		
		if (pri<15) {
			pri--
			offset:=17*pri
			MouseClick, left, 331, (268+offset) ;clica no field certo direto se <15
			sleep, 75
		}
		else {
			pri-=15
			while (pri!=0) {
				MouseClick, left, 441, 506 ; desce a barra de scroll
				sleep, 75
			    pri--
			}
			MouseClick, left, 331, 504 ; ultimo field
			
			sleep, 200
		}
		
		MouseClick, left, 445 , 270 ; clica no campo sub
		sleep, 100
		sub--
		offset:=17*sub
		MouseClick, left, 331, (293+offset)
		sleep, 100
		ImageSearch, px, py, 0, 0, 1000, 1000, *70 Images\Misc\teleportaraocampo.png
		MouseClick, left, px, py+2
		sleep, 500
		MouseClick, left, 604, 337 
		sleep, 3000
	}
	
	printMousePos(){
	MouseGetPos, X, Y
	msgbox, %x% . %y%  
	}


portToBlackKeep(){
	if (!isMapOpen()) {
		send, m
		}
	sleep, 50
	selectNorthEastAsia()
	sleep, 50
	MouseClick, right, 741, 422 ; clica em shoguncastle no mapa
	sleep, 500
	MouseMove, 853, 494, 100
	sleep, 500
	MouseMove, 988, 493, 100
	sleep, 500
	MouseClick,left, 992, 515, 100
}

portToMutants(){
	if (!isMapOpen()) {
		send, m
		}
	sleep, 50
	selectNorthEurope()
	sleep, 50
	MouseClick, right, 236, 223 ; 
	sleep, 500
	MouseMove, 324, 297, 100
	sleep, 500
	MouseMove, 446, 302, 100
	sleep, 500
	MouseClick,left, 452, 319, 100
}

selectNorthEastAsia(){
	mouseclick, left, 901, 162
	sleep, 20
	mouseclick, left, 836, 177 
}

selectNorthEurope(){
	mouseclick, left, 901, 162
	sleep, 20
	mouseclick, left, 836, 247 
}

isMapOpen(){
		ImageSearch, px, py, 861, 119, 1046, 252, *50 Images\Misc\mapa.png
		if (ErrorLevel=0){
			return, 1
		}
	return 0
		
}
	

isElementalistaActive(){
	ImageSearch, px, py, 461, 762, 930, 883, *70 Images\Mercenary\elementalista.png
	if (ErrorLevel=0) {
		return, 1 
		
	} 
	return, 0
}

isOracleActive(){
	ImageSearch, px, py, 461, 762, 930, 883, *50 Images\Mercenary\Oracle.png
	if (ErrorLevel=0) {
		return, 1 
	} 
	return, 0
}

isShamanActive(){
	ImageSearch, px, py, 461, 762, 930, 883, *50 Images\Mercenary\shaman.png
	if (ErrorLevel=0) {
		return, 1 
	} 
	return, 0
}

isMonkActive(){
	ImageSearch, px, py, 461, 762, 930, 883, *50 Images\Mercenary\Monk.png
	if (ErrorLevel=0) {
		return, 1 
	} 
	return, 0
}

isArtActive(){
	ImageSearch, px, py, 461, 762, 930, 883, *50 Images\Mercenary\art.png
	if (ErrorLevel=0) {
		return, 1 

	} 
	return, 0
}

isMainActive(){
	ImageSearch, px, py, 461, 762, 930, 883, *50 Images\Mercenary\Main.png
	if (ErrorLevel=0) {
		return, 1
	} 
	ImageSearch, px, py, 461, 762, 930, 883, *50 Images\Mercenary\Main2.png
	if (ErrorLevel=0) {
		return, 1 
		}
	return, 0
}

isLKActive(){
	ImageSearch, px, py, 461, 762, 930, 883, *50 Images\Mercenary\lk.png
	if (ErrorLevel=0) {
		return, 1 
	} 
	return, 0
}

isVikingActive(){
	ImageSearch, px, py, 461, 762, 930, 883, *50 Images\Mercenary\viking.png
	if (ErrorLevel=0) {
		return, 1 
	} 
	return, 0
}

inBattle(){
	ImageSearch, targetx, targety, 1100, 50, 1150 , 100, *50 Images\Misc\radar.png
	tooltip InBattle %ErrorLevel% ,700,0,6
	if (ErrorLevel=0 && getInCombat()=0) {
		return 0
	}
	return 1
}


disableAB(){

	
	if (getABStatus()=1){
		MouseClick, left, 1122, 661
		Mousemove, 100, 100
		sleep, 50
		}
	}


isABEnabled(){
	if (getABStatus()=1) {
	
		return, 1
	}
 return, 0
}

isSkirmishABEnabled(){
	ImageSearch, px, py, 900, 600, A_ScreenWidth, A_ScreenHeight , *20 Images\Misc\skirmishab.png
	if (Errorlevel=0){
		return, 1
	}
 return, 0
}


isABZero(){
	ImageSearch, px, py, 900, 600, A_ScreenWidth, A_ScreenHeight , *50 Images\Misc\zeroab.png
	if (Errorlevel=0){
		return, 1
	}
	ImageSearch, px, py, 900, 600, A_ScreenWidth, A_ScreenHeight , *50 Images\Misc\zeroab2.png
	if (Errorlevel=0){
		return, 1
	}
	ImageSearch, px, py, 900, 600, A_ScreenWidth, A_ScreenHeight , *70 Images\Misc\zeroab3.png
	if (Errorlevel=0){
		return, 1
	}
	ImageSearch, px, py, 900, 600, A_ScreenWidth, A_ScreenHeight , *70 Images\Misc\zeroab4.png
	if (Errorlevel=0){
		return, 1
	}
	
	
 return, 0
}

EnableAB(){
	if (!isABEnabled()){
		MouseClick, left, 1122, 661
		Mousemove, 100, 100
		sleep, 100
	}
}

EnableABSkirmish(){
	if (!isSkirmishABEnabled()){
		MouseClick, left, 1126, 723
		Mousemove, 100, 100
		sleep, 100
	}
}

CloseInventory(){
	ImageSearch, px, py, 400, 0, A_ScreenWidth, 500 , *16 Images\Misc\inv.png
	if (Errorlevel=0){
		MouseClick, left, 998, 90
	}
}

	deposit() {
	MouseClick, left, 970, 530
	sleep 100
	ImageSearch , tx, ty, 300, 200 , 900, 1000, *30 Images\Misc\depositar.png
	if (ErrorLevel=1) {
		MouseClick, left, 1130, 237
		sleep, 100
		MouseClick, left, 970, 480
		sleep, 200
		ImageSearch , tx, ty, 300, 200 , 900, 1000, *30 Images\Misc\depositar.png
		if (ErrorLevel=1){
			MouseClick, left, 1130, 287
			sleep, 100
			MouseClick, left, 970, 530
			sleep, 200
			ImageSearch , tx, ty, 300, 200 , 900, 1000, *30 Images\Misc\depositar.png
			if (ErrorLevel=1){
				MouseClick, left, 1130, 337
				sleep, 100
				MouseClick, left, 970, 620
				sleep, 200
				ImageSearch , tx, ty, 300, 200 , 900, 1000, *30 Images\Misc\depositar.png
				if (ErrorLevel=1){
					MouseClick, left, 1130, 387
					sleep, 100
					MouseClick, left, 970, 660
					sleep, 200
					ImageSearch , tx, ty, 300, 200 , 900, 1000, *30 Images\Misc\depositar.png
				}
			}
		}
	}
	MouseClick, left, 485, 333
	sleep 100
	Send, 1
	sleep 100
	MouseClick, left, 586, 385
	sleep 100
	MouseClick, left, 490, 410
	sleep 100
	MouseClick, left, 708, 77
}

	pedreiro() {
		send, {w down}
		sleep, 1500
		send {w up}
	}
	
	

useMPFood(){
	send, {space}
	sleep, 500
	ImageSearch, mpfoodx, mpfoody, 0, 0, 1200, 1000, *30 Images\Items\carpa.png
	if (ErrorLevel=0){
		MouseClick, right, mpfoodx, mpfoody
	}
	send, {space}
}

findLivingMob() {
	numMobs:=0
	lootableMobs:=0
	Global target
	global cursordelay

	loop 27 {
		x:=targetX(A_Index)
		y:=targetY(A_Index)
;		ImageSearch, targetx, targety, x-10,y-10, x+10 , y+10, *120 %target%
;		if (ErrorLevel=0) {
			numMobs+=1
			MouseMove x,y
			sleep, cursordelay
			if (ReadCursor()=2) {
				lootableMobs+=1
			}
			else if (ReadCursor()=1) {
				return, A_Index
			}
;		}
	}
	if (numMobs=0) {
		return, 0
	}
	else if (lootableMobs=numMobs) {
		return, -1
	}
	else {
		return, -2
	}
}

getAction(char, turn) {

	if (turn<0) {
		turn:=0
	}
	while (!ac:=action_%char%_%turn%) {
		if (turn=0) {
			return "attack"
		}
		else {
			turn-=1
		}
	}
	return ac
}

targetX(idx) {

	x:=x_%idx%
	return x 
}

targetY(idx) {

	y:=y_%idx%
	return y 
}


mapChars() {
	global
	Send, 1
	sleep 200
	Send, !f
	sleep 1000
	Loop %numberofmercs% {
		sleep 200
		local charNumber:=getActiveChar()
		max_hp_%charNumber%:=getHP(charNumber)
		local charName:=getMercName()
		merc_%charNumber%:=charName
		Loop 9 {
			if (charNumber=1) {
				ImageSearch, w, z, xf_top_left_%A_Index% , yf_top_left_%A_Index%, xf_bottom_right_%A_Index%, yf_bottom_right_%A_Index%, *50 Images\Formation\main1.png
				if (ErrorLevel=0) {
					pos_%charNumber%:=A_Index 
					break
				}
				ImageSearch, w, z, xf_top_left_%A_Index% , yf_top_left_%A_Index%, xf_bottom_right_%A_Index%, yf_bottom_right_%A_Index%, *50 Images\Formation\main2.png
				if (ErrorLevel=0) {
					pos_%charNumber%:=A_Index 
					break
				}
			}
			else {
				ImageSearch, w, z, xf_top_left_%A_Index% , yf_top_left_%A_Index%, xf_bottom_right_%A_Index%, yf_bottom_right_%A_Index%, *50 Images\Formation\%charName%.png
				if (ErrorLevel=0) {
					pos_%charNumber%:=A_Index 
					break
				}
			}
		}
		Send, {TAB}
	}
	sleep 500
	Send, !f
}

returnActiveMerc() {
	n:=getActiveChar()
	merc:=merc_%n%
	if (!merc) {
		merc:="unknown"
	}
	return merc
}
	
	